package com.yun.algorithmproblem.huaweiod;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class OD342 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] wealth = new int[n];
        for (int i = 0; i < n; i++) {
            wealth[i] = sc.nextInt();
        }
        @SuppressWarnings("unchecked")
        List<Integer>[] g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < n - 1; i++) {
            int parent = sc.nextInt()-1;
            int child = sc.nextInt()-1;
            g[parent].add(child);
        }
        int max = maxSum(n, wealth, g);
        System.out.println(max);
    }

    private static int maxSum(int n, int[] wealth, List<Integer>[] g) {
        int max = 0;
        for (int i = 0; i < n; i++) {
            int sum = wealth[i];
            for (int child : g[i]) {
                sum += wealth[child];
            }
            if (sum > max) {
                max = sum;
            }
        }
        return max;
    }
}
